package com.quizzapp.adapter;

import java.util.List;
import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.quizzapp.R;
import com.quizzapp.bean.Pregunta;
import com.quizzapp.bean.Respuesta;

public class QuestionAdapter extends ArrayAdapter<Pregunta> {

	private int resource;
	private LayoutInflater inflater;
	private Context context;

	public QuestionAdapter(Context ctx, int resourceId, List<Pregunta> objects) {

		super(ctx, resourceId, objects);
		this.resource = resourceId;
		this.inflater = LayoutInflater.from(ctx);
		this.context = ctx;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = (RelativeLayout) inflater.inflate(resource, null);
		Pregunta pregunta = getItem(position);
		TextView txtName = (TextView) convertView.findViewById(R.id.tv_questionName);
		txtName.setText(pregunta.getNombre());

		RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.list_responses);
		int count = 0;
		for (Respuesta respuesta : pregunta.getRespuestas()) {

			RadioButton radioButton = new RadioButton(this.context);
			radioButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			radioButton.setText(respuesta.getNombre());

			radioButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, 22);
			radioButton.setPadding(50, 0, 0, -5);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			params.setMargins(0, 0, 0, 0);
			radioButton.setLayoutParams(params);
			radioButton.setId(respuesta.getId());
			radioGroup.addView(radioButton, count);
			count++;
		}

		// ListView listViewResponse = (ListView)
		// convertView.findViewById(R.id.list_responses);
		// int height = pregunta.getRespuestas().size() * 65;
		// RelativeLayout.LayoutParams mParam = new
		// RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, height);
		// listViewResponse.setLayoutParams(mParam);
		// listViewResponse.setDivider(null);
		// listViewResponse.setAdapter(new ResponseAdapter(this.context,
		// R.layout.activity_response, pregunta.getRespuestas()));
		return convertView;
	}
}